//封装wx.request
let ajaxTimes = 0;

export const request = function (params) {

    ajaxTimes++;
    //设置显示加载中
    wx.showLoading({
        title: "加载中",
        mask: true
    });
    //设置公共路径
    let baseUrl = 'https://api-hmugo-web.itheima.net/api/public/v1'
    //判断是否传入请求头
    let header = {
        ...params.header
    }
    if (params.url.indexOf('/my/') !== -1) {
        header.Authorization = localStorage.getItem('token')
    }
    return new Promise((resolve, reject) => {
        wx.request({
            ...params,
            header: header,
            url: baseUrl + params.url,
            success: (reault) => {
                resolve(result.data.message)
            },
            fail: (error) => {
                reject(error)
            },
            complete: () => {
                ajaxTimes--;
                if (ajaxTimes === 0) {
                    //  关闭正在等待的图标
                    wx.hideLoading();
                }
            }
        })
    })
}